﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Zero.DataLayer;
using Zero.DataLayer.Repository;
using Zero.POCO.Compagne;
using Zero.POCO.Helper.Support;
using Zero.POCO.Support;
using Zero.Presenter;

namespace DevExpress.MailClient.Win.Forms.Planing
{
    public partial class frmEditOptimization : XtraForm, IOptimizationPlanView
    {
        public OptimizationPlanView _OptimizationPlanView { get; set; }
        public List<SupportView> _SupportViews { get; set; }
        public OptimizationPlanPresenter OptimizationPlanPresenter;

        public List<SupportView> SupportViewOpt { get; set; } 
        public frmEditOptimization(object optimizationObject,bool isNewObject,PlanTV plantv,PlanRadio planRadio,PlanPresse planPresse,string caption)
        {
            InitializeComponent();
            this.Text = caption;
            OptimizationPlanPresenter = new OptimizationPlanPresenter(this,new SupportPresseRepository(), new SupportTvRepository(), new SupportRadioRepository(), plantv,planRadio,planPresse );
            OptimizationPlanPresenter.Display();
            cbMedia.SelectedIndex = 0;
            var supportTv = _SupportViews.Where(sp => sp.Typemedia == TypeMedia.TV).ToList();
            sluSupport.Properties.DataSource = supportTv;
            sluSupport.Properties.DisplayMember = "Support";
            if (supportTv.Any())
                sluSupport.EditValue = supportTv.FirstOrDefault();

            SupportViewOpt = new List<SupportView>();
            gcSupport.DataSource = SupportViewOpt.ToList();


            for (int i = 0; i <     100; i++)
            {
                cbCouvGeneral.Properties.Items.Add(i + 1);
                cbRepGenerale.Properties.Items.Add(i + 1);
            }

            cbCouvGeneral.SelectedIndex = 23;
            cbRepGenerale.SelectedIndex = 2;
        }

        private void xtpgeneral_Paint(object sender, PaintEventArgs e)
        {

        }

        private void cbMedia_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cbMedia.Text)
            {
                case "Tele":
                {
                    var support = _SupportViews.Where(sp => sp.Typemedia == TypeMedia.TV).ToList();
                    
                    sluSupport.Properties.DataSource = support;
                    sluSupport.Properties.DisplayMember = "Support";
                    if (support.Any())
                        sluSupport.EditValue = support.FirstOrDefault();
                    break;
                }
                case "Radio":
                {
                    var support = _SupportViews.Where(sp => sp.Typemedia == TypeMedia.RD).ToList();
                    sluSupport.Properties.DataSource = support;
                    if (support.Any())
                        sluSupport.EditValue = support.FirstOrDefault();
                    break;
                }
                case "Presse":
                {
                    var support = _SupportViews.Where(sp => sp.Typemedia == TypeMedia.PR).ToList();
                    sluSupport.Properties.DataSource = support;
                    if (support.Any())
                        sluSupport.EditValue = support.FirstOrDefault();
                    break;
                }
            }


            
        }

        private void barButtonItem3_ItemClick(object sender, XtraBars.ItemClickEventArgs e)
        {
         

        }

    

        private void bbiAddsupportView_ItemClick(object sender, XtraBars.ItemClickEventArgs e)
        {
            var supportview = (SupportView)sluSupport.EditValue;
            if (!SupportViewOpt.Contains(supportview))
                SupportViewOpt.Add(supportview);
            gcSupport.DataSource = SupportViewOpt.ToList();


            gvSupport.Columns[1].Visible = false;
            gvSupport.Columns[3].Visible = false;
            gvSupport.Columns[4].Visible = false;
        }

        private void bbitEnleverSupportVew_ItemClick(object sender, XtraBars.ItemClickEventArgs e)
        {

        }

        private void sluSupport_Popup_1(object sender, EventArgs e)
        {
            sluSupport.Properties.View.Columns[1].Visible = false;
            sluSupport.Properties.View.Columns[3].Visible = false;
            sluSupport.Properties.View.Columns[4].Visible = false;
        }

        private void btnOptimize_Click(object sender, EventArgs e)
        {
            _OptimizationPlanView=new OptimizationPlanView();
            _OptimizationPlanView.SupportViews = SupportViewOpt;
            _OptimizationPlanView.Couverture = cbCouvGeneral.SelectedIndex + 1;
            _OptimizationPlanView.Repetition = cbRepGenerale.SelectedIndex + 1;
            _OptimizationPlanView.Prix = int.Parse(tePrix.Text);
            _OptimizationPlanView.DebutCompagne= dtdebut.Value;
            _OptimizationPlanView.FinCompagne = dtFin.Value;

            this.Close();
        }

        private void clbPlan_SelectedIndexChanged(object sender, EventArgs e)
        {
            var listeindidce = clbPlan.SelectedIndices;
            if (!listeindidce.Contains(0))
            {
                xtcTv.Enabled = false;
            }


            if (!listeindidce.Contains(1))
            {
                xtcRd.Enabled = false;
            }


            if (!listeindidce.Contains(2))
            {
                xtcPr.Enabled = false;
            }
        }


        
    }
}